/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importdiamond.service.impl;

import com.kmcsoft.goldshop.importdiamond.service.ImportDiamondService;
import com.kmcsoft.goldshop.model.master.ProductDiamond;
import com.kmcsoft.master.defaultservice.DefaultGenericService;
import com.kmcsoft.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import javax.jdo.Query;

/**
 *
 * @author huyvu
 */
public class JdoImportDiamondService extends DefaultGenericService implements ImportDiamondService {

    public ProductDiamond getProductDiamondById(long id) {
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_All");
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductStatusHistory_CreatedBy");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(ProductDiamond.class, id));
    }

    public ProductDiamond getRecentProduct(Date date, long empId) {
        Query query = getPersistenceManager().newQuery(ProductDiamond.class);
        query.setFilter("createdDate>=pDate && createdBy.id==pId && productType == pType");
        query.declareImports("import java.util.Date");
        query.declareParameters("Date pDate, Long pId, Integer pType");
        query.setOrdering("createdDate descending");
        Collection<ProductDiamond> objects = (Collection<ProductDiamond>) query.execute(DateUtil.beginOfDate(date), (Long) empId, (Integer) ProductDiamond.TYPE_DIAMOND);
        if (objects.isEmpty()) {
            return null;
        }
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_All");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(objects.iterator().next());
    }

    public Collection<ProductDiamond> getProductDiamonds() {
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_DiamondGroup");
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_DiamondColor");
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductDiamond_DiamondPure");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(ProductDiamond.class,"premiumJewelry == null && productType== pType","Integer pType",ProductDiamond.TYPE_DIAMOND));
    }
}
